﻿using UnityEngine;
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Reflection;
using System.Text.RegularExpressions;
namespace SG
{
    public class Util
    {
        /// <summary>
        /// 修改所有对象的layer
        /// </summary>
        static public void SetLayer(GameObject go, int layer)
        {
            go.layer = layer;
            foreach (Transform t in go.transform)
            {
                SetLayer(t.gameObject, layer);
            }
        }

        static public bool IsInternetReachable()
        {
            return Application.internetReachability != NetworkReachability.NotReachable;
        }
        static public bool IsInternetReachableViaCarrier()
        {
            return Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork;
        }
        static public bool IsInternetReachableViaWiFi()
        {
            return Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork;
        }

        public static int Int(object o)
        {
            return Convert.ToInt32(o);
        }

        public static float Float(object o)
        {
            return (float)Math.Round(Convert.ToSingle(o), 2);
        }

        public static long Long(object o)
        {
            return Convert.ToInt64(o);
        }

        public static int Random(int min, int max)
        {
            return UnityEngine.Random.Range(min, max);
        }

        public static float Random(float min, float max)
        {
            return UnityEngine.Random.Range(min, max);
        }

        public static string Uid(string uid)
        {
            int position = uid.LastIndexOf('_');
            return uid.Remove(0, position + 1);
        }

        /// <summary>
        /// 手机震动
        /// </summary>
        public static void Vibrate()
        {
            # if UNITY_ANDROIID||UNITY_IPHONE
            int canVibrate = PlayerPrefs.GetInt(GameDefines.AppPrefix + "Vibrate", 1);
            if (canVibrate == 1) Handheld.Vibrate();
            #endif
        }


        /// <summary>
        /// 清理内存
        /// </summary>
        public static void ClearMemory()
        {
            GC.Collect(); Resources.UnloadUnusedAssets();
            //LuaManager.Instance.LuaGCCollect();  
        }

        /// <summary>
        /// 网络可用
        /// </summary>
        public static bool NetAvailable
        {
            get
            {
                return Application.internetReachability != NetworkReachability.NotReachable;
            }
        }

        /// <summary>
        /// 是否是无线
        /// </summary>
        public static bool IsWifi
        {
            get
            {
                return Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork;
            }
        }

        /// <summary>
        /// 生成一个Key名
        /// </summary>
        public static string GetKey(string key)
        {
            return key;// Application.productName + GameDefines.UserId + "_" + key;
        }

        /// <summary>
        /// 取得整型
        /// </summary>
        public static int GetInt(string key)
        {
            string name = GetKey(key);
            return PlayerPrefs.GetInt(name);
        }

        /// <summary>
        /// 有没有值
        /// </summary>
        public static bool HasKey(string key)
        {
            string name = GetKey(key);
            return PlayerPrefs.HasKey(name);
        }

        /// <summary>
        /// 保存整型
        /// </summary>
        public static void SetInt(string key, int value)
        {
            string name = GetKey(key);
            PlayerPrefs.DeleteKey(name);
            PlayerPrefs.SetInt(name, value);
        }

        /// <summary>
        /// 取得数据
        /// </summary>
        public static string GetString(string key)
        {
            string name = GetKey(key);
            return PlayerPrefs.GetString(name);
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public static void SetString(string key, string value)
        {
            string name = GetKey(key);
            PlayerPrefs.DeleteKey(name);
            PlayerPrefs.SetString(name, value);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        public static void RemoveData(string key)
        {
            string name = GetKey(key);
            PlayerPrefs.DeleteKey(name);
        }

        /// <summary>
        /// 是否为数字
        /// </summary>
        public static bool IsNumber(string strNumber)
        {
            Regex regex = new Regex("[^0-9]");
            return !regex.IsMatch(strNumber);
        }

        /// <summary>
        /// 判断数字
        /// </summary>
        public static bool IsNumeric(string str)
        {
            if (str == null || str.Length == 0) return false;
            for (int i = 0; i < str.Length; i++)
            {
                if (!Char.IsNumber(str[i])) { return false; }
            }
            return true;
        }

        /// <summary>
        /// 是不是苹果平台
        /// </summary>
        /// <returns></returns>
        public static bool isApplePlatform
        {
            get
            {
                return Application.platform == RuntimePlatform.IPhonePlayer ||
                       Application.platform == RuntimePlatform.OSXEditor ||
                       Application.platform == RuntimePlatform.OSXPlayer;
            }
        }
        static private Dictionary<Type, Dictionary<object, string>> EnumStringCache = new Dictionary<Type, Dictionary<object, string>>();

        static private void InitDictEnum(Type type)
        {
            if (!EnumStringCache.ContainsKey(type))
            {
                Dictionary<object, string> dict = new Dictionary<object, string>();
                foreach (object v in Enum.GetValues(type))
                {
                    dict.Add(v, v.ToString());
                }
                EnumStringCache.Add(type, dict);
            }
        }

        static public string GetEnumString(Enum e)
        {
            Type type = e.GetType();
            InitDictEnum(type);
            return EnumStringCache[type][e];
        }

        static public object GetEnumValue(Type type, string eName)
        {
            if (doesEnumHaveName(type, eName))
            {
                return System.Enum.Parse(type, eName);
            }
            return null;
        }

        static public bool doesEnumHaveName(Type type, string eName)
        {
            InitDictEnum(type);
            return EnumStringCache[type].ContainsValue(eName);
        }
    }
}